﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RecruitingApplication.Data_Model;

namespace RecruitingApplication.ApplicationView
{
    public partial class ApplicationView : BasePage
    {
        //these values can be set on page load and accessed from page
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string HeardAbout { get; set; }
        public string Notes { get; set; }
        public int CandID { get; set; }
        public int HeardAboutID { get; set; }

        public bool IsCandSet { get; set; }
        public int AppsCount { get; set; }

        private void SetThisCandidate(int candID)
        {
            //SHOULD ONLY BE CALLED WHEN DB IS OPEN!!!!!!

            Candidate c = DataAccessLayer.GetCandidateByID(candID);

            if (c != null)
            {
                Name = c.Name;
                Email = c.Email;
                Phone = c.Phone;
                HeardAbout = c.HeardAbout.Name;
                HeardAboutID = c.HeardAboutID;
                CandID = candID;

                //if there are extra notes on how they heard about us, show them
                if (!string.IsNullOrWhiteSpace(c.HeardAboutNotes))
                {
                    Notes = c.HeardAboutNotes;
                }

                IsCandSet = true;
                AppsCount = c.Applications.Count;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            IsCandSet = false;
            AppsCount = 0;

            int? candID = null;
            int? appID = null;

            try
            {
                //attempt to get a candidate id or application id from the request url
                if (Request["candID"] != null)
                {
                    candID = Convert.ToInt32(Request["candID"]);
                }
                if (Request["appID"] != null)
                {
                    appID = Convert.ToInt32(Request["appID"]);
                }
            }
            catch { }

                

            //depending on which was id given, select the candidate from the db
            if (appID.HasValue)
            {
                DataAccessLayer.OpenDB();

                candID = DataAccessLayer.GetCandidateIDByAppID(appID.Value);
                SetThisCandidate(candID.Value);

                IQueryable data = DataAccessLayer.GetApplicationsForCandidate(candID.Value);

                this.ApplicationsRepeater.DataSource = data;
                this.TabsRepeater.DataSource = data;

                this.ApplicationsRepeater.DataBind();
                this.TabsRepeater.DataBind();

                DataAccessLayer.CloseDB();
            }
            else if (candID.HasValue)
            {
                DataAccessLayer.OpenDB();

                SetThisCandidate(candID.Value);

                IQueryable data = DataAccessLayer.GetApplicationsForCandidate(candID.Value);

                this.ApplicationsRepeater.DataSource = data;
                this.TabsRepeater.DataSource = data;

                this.ApplicationsRepeater.DataBind();
                this.TabsRepeater.DataBind();

                DataAccessLayer.CloseDB();
            }
            else
            {
                // Errors:
                // Search failed to find anyone.
            }
        }
     }
}